﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WPFRadio
{
	public class playlistbuilder
	{

       
        public static ToolStripMenuItem[] GeneratePlaylist()
        {

            List<ToolStripMenuItem> items = new List<ToolStripMenuItem>();

            var PlayList = playlist.LoadPlaylist();
            if (PlayList == null || PlayList.Radios.Count == 0)
            {
                return null;

            }

            var roots = from root in PlayList.Radios
                        where root.Parent.Equals(Guid.Empty)
                        select root;

            foreach (var root in roots)
            {
                var rootItem = new ToolStripMenuItem(root.Name);
                var children = from c in PlayList.Radios
                               where c.Parent == root.Id
                               select c;

                if (children.Count() > 0)
                {
                    foreach (var child in children)
                    {
                        var childItem = new ToolStripMenuItem(child.Name);
                        childItem.Tag = child;
                        rootItem.DropDownItems.Add(childItem);

                    }
                }
                else
                {
                    rootItem.Tag = root;

                }

                items.Add(rootItem);
            }
            return items.ToArray();
                 

        }

	}
}
